/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.dao.jpa;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.google.code.sapien.dao.CategoryAclDAO;
import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.CategoryAcl;
import com.google.code.sapien.model.Role;
import com.google.inject.Inject;

/**
 * A JPA implementation of the {@link com.google.code.sapien.dao.CategoryAclDAO} interface.
 * @author Adam
 * @version $Id$
 * 
 * Created on May 6, 2009 at 9:02:22 PM 
 */
public class CategoryAclJpaDAO extends AbstractJpaDAO<CategoryAcl> implements CategoryAclDAO {
	/**
	 * Constructs a category ACL JPA data access object.
	 * @param entityManager The entity manager.
	 */
	@Inject
	public CategoryAclJpaDAO(final EntityManager entityManager) {
		super(entityManager);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#get(com.google.code.sapien.model.Category,
	 *  com.google.code.sapien.model.Role)
	 */
	public CategoryAcl get(final Category category, final Role role) {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.getCategoryRole");
		query.setParameter("categoryId", category.getId());
		query.setParameter("roleId", role.getId());
		return (CategoryAcl) query.getSingleResult();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#list(com.google.code.sapien.model.Category)
	 */
	@SuppressWarnings("unchecked")
	public List<CategoryAcl> list(final Category category) {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.listCategory");
		query.setParameter("categoryId", category.getId());
		return query.getResultList();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#list(java.util.Collection)
	 */
	@SuppressWarnings("unchecked")
	public List<CategoryAcl> list(final Collection<Role> roles) {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.listRoles");
		query.setParameter("roles", roles);
		return query.getResultList();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#list(com.google.code.sapien.model.Role)
	 */
	@SuppressWarnings("unchecked")
	public List<CategoryAcl> list(final Role role) {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.listRole");
		query.setParameter("roleId", role.getId());
		return query.getResultList();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#listForGuests()
	 */
	@SuppressWarnings("unchecked")
	public List<CategoryAcl> listForGuests() {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.listForGuests");
		return query.getResultList();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.CategoryAclDAO#listForNewUsers()
	 */
	@SuppressWarnings("unchecked")
	public List<CategoryAcl> listForNewUsers() {
		final Query query = getEntityManager().createNamedQuery("categoryAcl.listForNewUsers");
		return query.getResultList();
	}

}
